<template>
  <div class="maintenance-page">
    <div class="breadcrumb">
      <Breadcrumb :data="breadcrumbData"></Breadcrumb>
    </div>
    <main>
      <div class="search-container">
        <div class="search-item">
          <div class="content">
            <el-input
              v-model="condition"
              placeholder="根据设备名称筛选"
              @blur="handleSearch"
              @keydown.native.enter="handleSearch">
            </el-input>
          </div>
<!--          <div class="label">维护时间：</div>-->
<!--          <div class="content">-->
<!--            <el-select placeholder="请选择审核状态" v-model="status" @change="handleSearch">-->
<!--              <el-option label="已提交" value="2"></el-option>-->
<!--              <el-option label="暂存" value="0"></el-option>-->
<!--              <el-option label="抄送" value="1"></el-option>-->
<!--            </el-select>-->
<!--          </div>-->
        </div>
        <div class="search-item">
          <el-button v-if="hasTabExport" type="primary" @click="handleExport">导出</el-button>
        </div>
      </div>
      <el-table
        class="table-container"
        :data="tableData"
        v-loading="tableLoading"
        element-loading-custom-class="custom-load"
        row-key="id">
        <el-table-column label="序号" type="index" width="120"></el-table-column>
        <el-table-column label="设备名称" prop="deviceName"></el-table-column>
        <el-table-column label="设备型号" prop="deviceModel"></el-table-column>
        <el-table-column label="设备编号" prop="deviceNo"></el-table-column>
        <el-table-column label="进场日期" prop="marchDate"></el-table-column>
        <el-table-column label="维护人员" prop="maintainersName"></el-table-column>
        <el-table-column label="维护周期" prop="maintainCycle"></el-table-column>
        <el-table-column label="维护时间" prop="maintainDate"></el-table-column>
        <el-table-column label="维护附件" prop="maintainImagesUrl">
          <template slot-scope="scope">
            <el-image
              v-if="/\.jpg|\.jpeg|\.png/.test(scope.row.maintainImagesUrl)"
              class="img"
              :src="$checkUrl(scope.row.maintainImagesUrl)"
              :preview-src-list="[$checkUrl(scope.row.maintainImagesUrl)]">
            </el-image>
            <span
              v-else
              class="file-name"
              @click="handleDownLoad(scope.row.maintainImagesUrl)">
              {{ setFileName(scope.row.maintainImagesUrl) }}
            </span>
          </template>
        </el-table-column>
<!--        <el-table-column label="操作" width="100">-->
<!--          <template slot-scope="scope">-->
<!--            <span class="btn" @click="handleEdit(scope.row)">查看</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <div class="empty-content" slot="empty">
          <i class="iconfont iconwushuju"></i>
          <p>暂无数据</p>
        </div>
      </el-table>
      <el-pagination
        v-if="total > 10"
        :page-size="pageSize"
        :current-page="pageNo"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </main>
  </div>
</template>

<script>
import Breadcrumb from '@/components/public/Breadcrumb';

export default {
  data() {
    return {
      breadcrumbData: [
        { name: '机械设备' },
        { name: '维保记录' }
      ],
      // 列表数据
      tableData: [],
      tableLoading: false,
      total: 0,
      pageSize: 10,
      pageNo: 1,
      hasSearch: false,
      visible: false,
      type: 'add',
      currentData: {},
      condition: '',
      status: '1',
      btnLaoding: false
    };
  },
  computed: {
    projectId() {
      return this.$store.state.currentProjectId;
    },
    userInfo() {
      return this.$store.state.userInfo;
    }
  },
  async created() {
    this.getTableData();
  },
  components: {
    Breadcrumb,
  },
  methods: {
    // 获取列表数据
    async getTableData() {
      this.tableLoading = true;
      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,
        projectId: this.projectId,
      };
      if (this.hasSearch) {
        params.condition = this.condition;
      }
      const { success, data: { data }} = await this.$Model.getMaintainList(params);
      this.tableLoading = false;
      if (success && data) {
        this.tableData = data.list || [];
        this.total = data.total || 0;
      }
    },
    /**
     * pageSize 改变时会触发
     * @param pageSize 每页条数
     */
    handleSizeChange(pageSize) {
      this.pageNo = 1;
      this.pageSize = pageSize;
      this.getTableData();
    },
    /**
     * currentPage 改变时会触发
     * @param pageNo 当前页
     */
    handleCurrentChange(pageNo) {
      this.pageNo = pageNo;
      this.getTableData();
    },
    // 清除参数
    clear() {
      this.pageNo = 1;
      this.pageSize = 10;
      this.total = 0;
      this.tableLoading = false;
      this.tableData = [];
    },
    // 搜索
    handleSearch() {
      this.clear();
      this.hasSearch = !!this.condition;
      this.getTableData();
    },
    // 下载附件
    handleDownLoad(url) {
      window.open(this.$checkUrl(url));
    },
    // 导出当前页面数据
    handleExport() {
      // const dateStr = this.$moment().format('YYYY-MM-DD');
      const href = `${this.$configs.baseURL}/bus/mach/tsg/downLoadXlsByJxl`;
      const params = {
        projectId: this.projectId,
        pageSize: this.pageSize,
        pageNum: this.pageNo,
        // startDate: dateStr,
        // endDate: dateStr
      };
      let urlStr = '';
      Object.keys(params).forEach((key, index) => {
        const value = params[key] === undefined ? '' : params[key];
        urlStr += `${index === 0 ? '?' : '&'}${key}=${value}`;
      });
      window.open(href + urlStr);
    },
    // 查看
    handleEdit(row) {}
  }
};
</script>

<style scoped lang="scss">
  @import '../../../assets/styles/table';

  .maintenance-page {
    width: 100%;
    height: 100%;
    background-color: $mainBg;
    overflow: hidden;
    main {
      width: 100%;
      height: calc(100% - 50px);
      padding: 20px;
      .search-container {
        width: 100%;
        display: flex;
        justify-content: space-between;
        margin-bottom: 20px;
        .search-item {
          display: flex;
          align-items: center;
          .el-input {
            width: 300px;
            margin-right: 20px;
          }
        }
        .search-btn {
          margin-left: 20px;
        }
      }
      .table-container {
        height: calc(100% - 120px);
        /deep/ .el-table__body-wrapper {
          height: calc(100% - 50px);
          padding-bottom: 46px;
          overflow-y: auto;
          .btn {
            padding: 0 5px;
            cursor: pointer;
            color: $mainColor;
          }
          .img {
            width: 80px;
            height: 80px;
          }
        }
        /deep/ .el-table__header {
          th.is-leaf .el-button span {
            color: #fff;
          }
        }
      }

    }
  }
</style>
